package com.rmb.signin.service.impl;

import com.rmb.common.model.response.CommonCode;
import com.rmb.common.model.response.ResponseResult;
import com.rmb.mart.entity.userinfo.UserCompany;
import com.rmb.mart.entity.userinfo.UserInfo;
import com.rmb.mart.utils.RandomCode;
import com.rmb.mart.utils.SendSms;
import com.rmb.signin.dao.YxsUserInfoDao;
import com.rmb.signin.service.MyUserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName MyUserInfoServiceImpl
 * @Description 注册登入密码找回
 * @Author yxs
 * @Date 2021/1/25 9:23
 **/
@Service
public class MyUserInfoServiceImpl implements MyUserInfoService {

    //状态未审核
    private static  final Integer NOTREVIEWED = 3;
    @Autowired(required = false)
    private YxsUserInfoDao yUserInfoDao;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查询所有数据
     * @return
     */
    @Override
    public List<UserInfo> selectAll() {
        List<UserInfo> list = this.yUserInfoDao.selectAll();
        return list;
    }

    /**
     * 发送手机短信
     * @param phone
     * @return
     */
    @Override
    public ResponseResult sendMessage(String phone) {
        if (phone!=null){
            //获取验证码
            String code = RandomCode.genCode();
            //将验证码存储到redis缓存中
            ValueOperations valueOperations = redisTemplate.opsForValue();
            //通过key，value形式存储
            valueOperations.set("code",code);
            //过期时间设置
            redisTemplate.expire("code",60000, TimeUnit.MILLISECONDS);
            //发送短信
            SendSms.sendMag(phone, code);

            return new ResponseResult(CommonCode.SENT_SUCCESSFULLY);
        }
        return new ResponseResult(CommonCode.PARAM_FALL);
    }

    /**
     * 查看验证码
     */
    @Override
    public String getcode() {
        ValueOperations valueOperations = redisTemplate.opsForValue();
        String code = (String) valueOperations.get("code");
        return code;
    }

    /**
     * 注册
     * @param userInfo
     * @return
     */
    @Override
    public ResponseResult register(String code,UserInfo userInfo) {
        ValueOperations valueOperations = redisTemplate.opsForValue();
        String code1 = (String) valueOperations.get("code");
        System.out.println(code1);
        System.out.println(code);
        if (code.equals(code1)){
            //根据手机进行判断
            String userPhone = userInfo.getUserPhone();
            List<UserInfo> list = yUserInfoDao.selectFindByPhone(userPhone);
            if (list==null){
                yUserInfoDao.register(userInfo);
                return new ResponseResult(CommonCode.ADDED_SUCCESSFULLY);
            }
            return new ResponseResult(CommonCode.PHONE_NUMBER);
        }else {
            return new ResponseResult(CommonCode.VERIFICATION_NOT);
        }

    }
    /**
     * 完善信息
     * @param userCompany
     * @return
     */
    @Override
    public int addcompany(UserCompany userCompany) {
        return yUserInfoDao.addcompany(userCompany);
    }

    /**
     * 查询完善信息表
     * @return
     */
    @Override
    public List<UserCompany> selectCompany() {
        return yUserInfoDao.selectCompany();
    }

    /**
     * 短信验证登入
     * @param logincode
     * @param userInfo
     * @return
     */
    @Override
    public ResponseResult login(String logincode, UserInfo userInfo) {
        ValueOperations valueOperations = redisTemplate.opsForValue();
        String code = (String) valueOperations.get("code");
        if (code.equals(logincode)){
            String userPhone = userInfo.getUserPhone();
            System.out.println(userPhone);
            String password = userInfo.getPassword();
            System.out.println(password);
            UserInfo login = yUserInfoDao.login(userInfo);
            System.out.println(login);
            if (login!=null){
                return new ResponseResult(CommonCode.SUCCESS);
            }else {
                return new ResponseResult(CommonCode.PHONE_NUMBER);
            }
        }else {
            return new ResponseResult(CommonCode.VERIFICATION_NOT);
        }

    }
    /**
     * 通过短信验证的方式进行修改密码
     * @param userPhone
     * @param retrieveCode
     * @param xpassword
     * @param qpassword
     * @return
     */
    @Override
    public ResponseResult retrievePassword(String userPhone, String retrieveCode, String xpassword, String qpassword) {
        ValueOperations valueOperations = redisTemplate.opsForValue();
        String code = (String) valueOperations.get("code");
        if (retrieveCode.equals(code)){
            if (xpassword.equals(qpassword)){
                System.out.println(xpassword);
                System.out.println(qpassword);
                yUserInfoDao.updatePassword(userPhone,xpassword);
                return new ResponseResult(CommonCode.SUCCESS);
            }else {
                return new ResponseResult(CommonCode.PASSWORDTWO_NOT);
            }
        }else {
            return new ResponseResult(CommonCode.FAIL);
        }


    }

    /**
     * 企业审核通过
     * @param id
     * @return
     */
    @Override
    public ResponseResult toexaminetrue(Long id) {
        UserCompany userCompany = yUserInfoDao.findById(id);
        int status = (int) userCompany.getStatus();
        if (status==NOTREVIEWED){
            yUserInfoDao.updateStatustrue(id);
            return new ResponseResult(CommonCode.SUCCESS);
        }else {
            return new ResponseResult(CommonCode.FAIL);
        }

    }

    /**
     * 企业审核不通过
     */
    @Override
    public ResponseResult toexaminefalse(Long id) {
        UserCompany byid = yUserInfoDao.findById(id);
        int status = (int) byid.getStatus();
        if (status==NOTREVIEWED){
            yUserInfoDao.updateStatusfalse(id);
            return new ResponseResult(CommonCode.SUCCESS);
        }else {
            return new ResponseResult(CommonCode.FAIL);
        }
    }

}
